package com.mobi.Survey.model;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * DBHelper.java Creation of Tables in database
 * 
 */
public class DBHelper extends SQLiteOpenHelper
{

	private static final String DATABASE_NAME = "db_survey.db";
	private static final int DATABASE_VERSION = 2;

	public DBHelper(Context context)
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);

	}

	@Override
	public void onCreate(SQLiteDatabase db)
	{
		TableFactory.getFactory().createTable(new SurveyDetailTable(), db);
		TableFactory.getFactory().createTable(new SurveyResultTable(), db);
		TableFactory.getFactory().createTable(new SurveyTable(), db);
		TableFactory.getFactory().createTable(new SubscriptionTable(), db);
		TableFactory.getFactory().createTable(new AggregateResultTable(), db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{

		if (oldVersion >= newVersion)
			return;

		String sql = null;
		if (oldVersion == 1)
		{
			sql = "ALTER TABLE " + SurveyDetailTable.TABLE + " ADD "
					+ SurveyDetailTable.COL_CTRL_WIDTH + " integer DEFAULT 0 ";
			db.execSQL(sql);
			sql = "ALTER TABLE " + SurveyDetailTable.TABLE + " ADD "
					+ SurveyDetailTable.COL_CTRL_HEIGHT + " integer DEFAULT 0 ";
			db.execSQL(sql);

			sql = "ALTER TABLE " + SurveyTable.TABLE + " ADD "
					+ SurveyTable.COL_BG_COLOR + " text DEFAULT null ";
			db.execSQL(sql);

			sql = "ALTER TABLE " + SurveyTable.TABLE + " ADD "
					+ SurveyTable.COL_BG_IMAGE_URL + " text DEFAULT null ";
			db.execSQL(sql);

			sql = "ALTER TABLE " + SurveyTable.TABLE + " ADD "
					+ SurveyTable.COL_FONT_COLOR + " text DEFAULT null ";
			db.execSQL(sql);

			Log.d("EventsData", "onUpgrade	: " + sql);
		}

	}

}
